import Vue from 'vue'
import {Toast} from 'mint-ui'
// 更新个人基本信息
export const updatePinformation = function (context) {
  let axios = Vue.prototype.$Axios
  let data = context.state.baseInfoMessageAdd
  let formData = new FormData()
  formData.append('userID', context.state.userID)
  if(data.file){
    formData.append('file', data.file)
  }
  if(data.nickName){
    formData.append('nickName', data.nickName)
  }
  if(data.city){
    formData.append('city', data.city)
  }
  if(data.company){
    formData.append('company', data.company)
  }
  if(data.direction){
    formData.append('direction', data.direction)
  }
  if(data.direction){
    formData.append('direction', data.direction)
  }
  if(data.position){
    formData.append('position', data.position)
  }
  if(data.status){
    formData.append('status', data.status)
  }
  if(data.sex){
    formData.append('sex', data.sex)
  }
  if(data.age){
    formData.append('age', data.age)
  }
  if(data.workLength){
    formData.append('workLength', data.workLength)
  }
  axios.post(`/api/user/updateBasicInfo`, formData)
    .then(res => {
      context.dispatch('action_getPInformation')
    }).catch(err => {
  }).finally(function () {
    context.commit('setPInformationAdd')
  })
}
// 获取个人基本信息
export const getPinformation = function (context) {
  let axios = Vue.prototype.$Axios
  axios.get(`/api/user/userInfo/${context.state.userID}`)
    .then(res => {
      if (res.status === 200) {
        setTimeout(function(){
          const proInfoDetail = res.data.proInfoDetail
          context.state.baseInfoMessage.image = proInfoDetail.detailTop.image
          context.state.baseInfoMessage.userID = proInfoDetail.detailTop.userID
          context.state.baseInfoMessage.basicInfoID = proInfoDetail.detailTop.basicInfoID
          context.state.baseInfoMessage.nickName = proInfoDetail.detailTop.nickName
          context.state.baseInfoMessage.status = proInfoDetail.detailTop.status
          context.state.baseInfoMessage.age = proInfoDetail.perIntro.age
          context.state.baseInfoMessage.sex = proInfoDetail.perIntro.sex
          context.state.baseInfoMessage.workLength = proInfoDetail.perIntro.workLength
          context.state.baseInfoMessage.city = proInfoDetail.perIntro.city
          context.state.baseInfoMessage.company = proInfoDetail.detailTop.company
          context.state.baseInfoMessage.position = proInfoDetail.detailTop.position
          context.state.baseInfoMessage.directionName = proInfoDetail.detailTop.directionName
          context.state.baseInfoMessage.direction = proInfoDetail.detailTop.direction
          context.state.introduction = proInfoDetail.perIntro.introduction
          context.state.workExplain.workTime = proInfoDetail.resState.workTime
          context.state.workExplain.workAddress = proInfoDetail.resState.workAddress
          context.state.workExplain.price = proInfoDetail.detailTop.price
          context.state.workExperiment = []
          proInfoDetail.workExper.forEach(function (workExper) {
            context.state.workExperiment.push({
              company: workExper.company,
              experienceDesc: workExper.experienceDesc,
              position: workExper.position,
              startTime: workExper.startTime?workExper.startTime.substring(0, 10):null,
              endTime: workExper.endTime?workExper.endTime.substring(0, 10):null,
              workCertificate: workExper.workCertificate,
              basicInfoID: workExper.basicInfoID
            })
          })
          context.state.educationExperiment = []
          proInfoDetail.eduExper.forEach(function (eduExper) {
            context.state.educationExperiment.push({
              basicInfoID: eduExper.basicInfoID,
              endTime: eduExper.endTime ? eduExper.endTime.substring(0, 10): null,
              experienceDesc: eduExper.experienceDesc,
              qualification: eduExper.qualification,
              school: eduExper.school,
              specialty: eduExper.specialty,
              startTime: eduExper.startTime?eduExper.startTime.substring(0, 10):null
            })
          })
          context.state.skillList = []
          proInfoDetail.skillList.forEach(function(skill){
            context.state.skillList.push(skill)
          })
          context.state.projectList = []
          proInfoDetail.projectList.forEach(function (project) {
            context.state.projectList.push(project)
          })
        },0)
      }
    }).catch(err => {
    console.log("err")
  }).finally(function(){
    console.log("finally")
  })
}
// 更新工作说明
export const updateWorkExplain = function (context, data) {
  let axios = Vue.prototype.$Axios
  data.userID = context.state.userID
  axios.post(`/api/user/updateJobRequirement`, data)
    .then(res => {
      context.dispatch('action_getPInformation')
    }).catch(err => {
  })
}

// 更新个人介绍
export const updateIntroduction = function (context, data) {
  let axios = Vue.prototype.$Axios
  data.userID = context.state.userID
  axios.post(`/api/user/updateIntroduction`, data)
    .then(res => {
      if (res.data && res.data.msg === '更新成功') {
        context.commit('setIntroduction', data.introduction)
      }
    }).catch(err => {
    Toast({
      message: '网络好像出错了',
      position: 'middle',
      duration: 1000
    });
  })
}

// 添加工作经历
export const addWorkExperiment = function (context, data) {
  let axios = Vue.prototype.$Axios
  data.basicInfoID = context.state.baseInfoMessage.basicInfoID
  var formData = new FormData()
  formData.append('basicInfoID', data.basicInfoID)
  if(data.company){
    formData.append('company', data.company)
  }
  if(data.position){
    formData.append('position', data.position)
  }
  if(data.startTime){
    formData.append('startTime', data.startTime)
  }
  if(data.endTime){
    formData.append('endTime', data.endTime)
  }
  if(data.experienceDesc){
    formData.append('experienceDesc', data.experienceDesc)
  }
  if(data.workCertificate){
    formData.append('workCertificate', data.workCertificate)
  }
  if(data.showFirst){
    formData.append('showFirst', data.showFirst)
  }
  if(data.file){
    formData.append('file', data.file)
  }
  axios.post(`/api/user/addWorkExp`, formData)
    .then(res => {
      if (res.data && res.data.msg === '更新成功') {
        context.dispatch('action_getPInformation')
      }
    }).catch(err => {
    console.log(err)
  })
}
//  添加教育经历
export const addEduction = function (context, data) {
  let axios = Vue.prototype.$Axios
  data.basicInfoID = context.state.baseInfoMessage.basicInfoID
  axios.post(`/api/user/addEduExp`, data)
    .then(res => {
      if (res.data && res.data.msg === '添加成功') {
        context.state.educationExperiment.push(data)
      }
    }).catch(err => {

  })
}
// 上传头像
export const uploadAvatar = function(context, data){
  let axios = Vue.prototype.$Axios
  let formData = new FormData()
  formData.append('file', data)
  formData.append('userID', context.state.userID)
  axios.post(`/api/user/updateBasicInfo`,formData, {
    headers:{
      'Content-Type' : 'application/json;charset=utf-8'
    }
  })
    .then(res => {
      context.dispatch('action_getPInformation')
    }).catch(err => {
    Toast({
      message: '网络好像出错了',
      position: 'middle',
      duration: 1000
    });
  }).finally(function () {
    context.commit('setPInformationAdd')
  })
}
// 添加技能
export const addSkill = function (context, data) {
  let axios = Vue.prototype.$Axios
  data.basicInfoID = context.state.baseInfoMessage.basicInfoID
  axios.post(`/api/user/addSkill`, data)
    .then(res => {
      if (res.data && res.data.msg === '新增成功') {
        context.dispatch('action_getPInformation')
      }
    }).catch(err => {
  })
}
export const deleteSkill = function(context, data){
  let axios = Vue.prototype.$Axios
  let formdata = new FormData()
  formdata.append('id', data.id)
  axios.post(`/api/user/delSkill`,formdata).then(res => {
    if (res.data && res.data.msg === '操作成功') {
      context.state.skillList.splice(data.index, 1);
    }
  }).catch(err => {

  })
}
export const addProduction = function(context, data) {
  let axios = Vue.prototype.$Axios
  data.basicInfoID = context.state.baseInfoMessage.basicInfoID
  var formdata = new FormData()
  formdata.append('basicInfoID', data.basicInfoID)
  if(data.projectName){
    formdata.append("projectName", data.projectName)
  }
  if(data.projectLink){
    formdata.append("projectLink", data.projectLink)
  }
  if(data.industry){
    formdata.append("industry", data.industry)
  }
  if(data.function){
    formdata.append("function", data.function)
  }
  if(data.infoDesc){
    formdata.append("infoDesc", data.infoDesc)
  }
  if(data.duty){
    formdata.append("duty", data.duty)
  }
  axios.post(`/api/user/addProject`,formdata).then(res => {
   console.log(res)
  }).catch(err => {

  })
}

